package org.iks.store.business.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.iks.common.AjaxResult;
import org.iks.common.pojo.message.ChatMessage;
import org.iks.common.pojo.message.ChatMessageStoreVo;
import org.iks.common.util.JwtUtil;
import org.iks.common.util.ServletUtil;
import org.iks.store.business.service.SessionMessageService;
import org.iks.store.business.vo.MessageQueryVo;
import org.iks.store.business.vo.message.MessageAnchorQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * Author kzou
 * Date 2024/2/20
 * Description:
 */
@Tag(name = "消息模块")
@RestController
@RequestMapping("message")
public class MessageController {

    @Autowired
    private SessionMessageService messageService;

    @Operation(summary = "保存消息")
    @PostMapping("save/chatMessage")
    public AjaxResult<Void> saveChatMessage(@RequestBody ChatMessageStoreVo message) {
        messageService.saveMessage(message);
        return AjaxResult.success();
    }

    @Operation(summary = "消息已读标记")
    @PostMapping("/read/remark")
    public AjaxResult<?> readMessage(Integer messageId) {
        String token = ServletUtil.getToken();
        String userAccount = JwtUtil.getUserAccount(token);
        int res = messageService.readMessage(userAccount,messageId);
        return AjaxResult.success(res);
    }

    @Operation(summary = "信息获取")
    @PostMapping("query/session/chatMessage")
    public AjaxResult<List<ChatMessage>> querySessionMessage(@RequestBody MessageQueryVo queryVo) {
        String token = ServletUtil.getToken();
        String userAccount = JwtUtil.getUserAccount(token);
        List<ChatMessage> result = messageService.querySessionMessage(queryVo,userAccount);
        return AjaxResult.success(result);
    }

    @PostMapping("/query/anchor")
    public AjaxResult<List<ChatMessage>> querySessionByAnchor(@RequestBody MessageAnchorQueryVo queryVo) {
        String token = ServletUtil.getToken();
        String userAccount = JwtUtil.getUserAccount(token);
        List<ChatMessage> result = messageService.querySessionMessageByAnchor(userAccount, queryVo);
        return AjaxResult.success(result);
    }

    @Operation(summary = "消息详情")
    @PostMapping("query/message/id")
    public AjaxResult<ChatMessage> queryById(Integer messageId) {
        String token = ServletUtil.getToken();
        String userAccount = JwtUtil.getUserAccount(token);
        ChatMessage data = messageService.queryById(messageId, userAccount);
        return AjaxResult.success(data);
    }

    @Operation(summary = "获取最近一条消息")
    @PostMapping("query/message/latest")
    public AjaxResult<ChatMessage> queryLatestMessage(Integer sessionId) {
        var result = messageService.queryLatestMessage(sessionId);
        return AjaxResult.success(result);
    }


}
